import {listGoods,countGoods} from "../../../util/axios"
const state={
    goodslist:[],
    page:1,
    size:2,
    total:0
}
const getters={
    //获取商品列表
    getGoods(state){
      return state.goodslist
    },
    getSize(state){
        return state.size
    },
    getPage(state){
        return state.page
    },
    getTotal(state){
        return state.total
    }
  
}
const mutations={
    //获取列表赋值给数据
    REQ_GOODLIST(state,payload){
       state.goodslist = payload
    },
    //改变Total
    REQ_TOTAL(state,payload){
       state.total = payload
    },
    //改变page
    REQ_PAGE(state,payload){
        state.page = payload
    }
}
const actions={
    //列表接口
     getGoodslistACtion(context){
        listGoods({
            size:context.state.size,
            page:context.state.page,
        }).then(res=>{
            if(res.code == 200){
                let list = res.list ? res.list:[];
                if(context.state.page >1 &&list.length==0){
                    context.state.page--;
                    context.dispatch("getGoodslistACtion");
                }
                context.commit("REQ_GOODLIST",list);
            }
        })
     },
     //获取总数
     getGoodsCount({commit}){
        countGoods().then(res=>{
           commit("REQ_TOTAL",res.list[0].total)
        })
     },
     //页面改变时候，重新调取页面数据接口
     currentChange(context,data){
         context.commit("REQ_PAGE",data);
         context.dispatch("getGoodslistACtion");
     }
}


export default{
    state,
    getters,
    mutations,
    actions,
    namespaced:true
}